package Service;

import Dao.ArticleAttachmentDAO;
import Dao.ArticleDAO;
import Dao.CommentDao;
import Dao.Impl.*;
import Dao.UserDAO;
import Dto.CommentDto;
import entity.Comment;
import entity.User;
import exception.BusinessException;
import sun.security.krb5.internal.PAData;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

/**
 * @Author Su
 * @Date 2025/5/8 9:31
 * @Version 1.0 （版本号）
 */
public class CommentService {

    private CommentDao commentDao;

    private UserDAO userDAO;

    public CommentService(Connection connection) {
        this.commentDao = new CommentDaoImpl(connection);
        this.userDAO = new UserDAOImpl(connection);
    }

    public CommentDto getCommentById(Integer parentId) throws BusinessException, SQLException {

        CommentDto comment = commentDao.getCommentById(parentId);
        if (comment == null){
            throw new BusinessException("评论不存在");
        }
        return comment;
    }

    public boolean addComment(Comment commentReq) throws BusinessException, SQLException {

        boolean result = commentDao.addComment(commentReq);
        if (!result){
            throw new BusinessException("添加评论失败");
        }
        return true;
    }

    public List<Comment> selectComment(CommentDto commentReq) throws SQLException{

        List<Comment> comments = commentDao.getComments(commentReq);
        for (Comment comment : comments) {
            //查找评论者的用户名
            System.out.println("查找评论者的用户名");
            User user = userDAO.getUserByUserId(comment.getUserId());
            comment.setUsername(user.getUsername());
        }
        return comments;
    }
}
